// pages/infodesc/infodesc.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stateArray: ['待开工', '施工中', '政策停工', '已完工'],
  },
  // 获取地址前缀
  getAddress() {
    var IAP = getApp().globalData.host;
    this.setData({
      IAP: IAP
    })
  },
  // 选择施工状态
  stateChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  // 添加开工图片
  startAdd(e) {
    wx.chooseImage({
      success: res => {
        var startImageArr = [];
        res.tempFilePaths.forEach((value, index) => {
          wx.uploadFile({
            url: this.data.IAP + 'oss/upload',
            filePath: value,
            name: 'file',
            success: res => {
              startImageArr.push(JSON.parse(res.data).url[0]);
            },
          })
        })
        setTimeout(() => {
          if (this.data.startImageArr) {
            this.setData({
              startImageArr: this.data.startImageArr.concat(startImageArr).splice(0, 9)
            })
          } else {
            this.setData({
              startImageArr: startImageArr
            })
          }
          var url = this.data.IAP
          if (this.data.electricType == '电杆' || this.data.electricType == '电缆') {
            url = url + 'electric/update'
          } else if (this.data.electricType == '连接线' || this.data.electricType == '架空线') {
            url = url + 'cable/update'
          }
          wx.request({
            url: url,
            header: {
              "token": wx.getStorageSync('token')
            },
            method: "POST",
            data: {
              electricId: this.data.electricId,
              startAnnex: this.data.startImageArr.join(',')
            },
            success: res => {
              if (res.data.code == 0) {
                wx.showToast({
                  title: '上传成功',
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            }
          })
        }, 500)
      },
    })
  },
  // 删除开工图片
  startClose(e) {
    var currentIndex = e.currentTarget.dataset.index;
    this.data.startImageArr.splice(currentIndex, 1)
    this.setData({
      startImageArr: this.data.startImageArr
    })
    setTimeout(() => {
      var url = this.data.IAP
      if (this.data.electricType == '电杆' || this.data.electricType == '电缆') {
        url = url + 'electric/update'
      } else if (this.data.electricType == '连接线' || this.data.electricType == '架空线') {
        url = url + 'cable/update'
      }
      wx.request({
        url: url,
        method: "POST",
        header: {
          "token": wx.getStorageSync('token')
        },
        data: {
          electricId: this.data.electricId,
          startAnnex: this.data.startImageArr.join(',')
        },
        success: res => {
          if (res.data.code == 0) {
            wx.showToast({
              title: '删除成功',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }, 500)
  },
  // 添加施工中图片
  constructionAdd(e) {
    wx.chooseImage({
      success: res => {
        var constructionImageArr = [];
        res.tempFilePaths.forEach((value, index) => {
          wx.uploadFile({
            url: this.data.IAP + 'oss/upload',
            filePath: value,
            name: 'file',
            success: res => {
              constructionImageArr.push(JSON.parse(res.data).url[0]);
            },
          })
        })
        setTimeout(() => {
          if (this.data.constructionImageArr) {
            this.setData({
              constructionImageArr: this.data.constructionImageArr.concat(constructionImageArr).splice(0, 9)
            })
          } else {
            this.setData({
              constructionImageArr: constructionImageArr
            })
          }
          var url = this.data.IAP
          if (this.data.electricType == '电杆' || this.data.electricType == '电缆') {
            url = url + 'electric/update'
          } else if (this.data.electricType == '连接线' || this.data.electricType == '架空线') {
            url = url + 'cable/update'
          }
          wx.request({
            url: url,
            header: {
              "token": wx.getStorageSync('token')
            },
            method: "POST",
            data: {
              electricId: this.data.electricId,
              underAnnex: this.data.constructionImageArr.join(',')
            },
            success: res => {
              if (res.data.code == 0) {
                wx.showToast({
                  title: '上传成功',
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            }
          })
        }, 500)
      },
    })
  },
  // 删除施工中图片
  constructionClose(e) {
    var currentIndex = e.currentTarget.dataset.index;
    this.data.constructionImageArr.splice(currentIndex, 1)
    this.setData({
      constructionImageArr: this.data.constructionImageArr
    })
    setTimeout(() => {
      var url = this.data.IAP
      if (this.data.electricType == '电杆' || this.data.electricType == '电缆') {
        url = url + 'electric/update'
      } else if (this.data.electricType == '连接线' || this.data.electricType == '架空线') {
        url = url + 'cable/update'
      }
      wx.request({
        url: url,
        header: {
          "token": wx.getStorageSync('token')
        },
        method: "POST",
        data: {
          electricId: this.data.electricId,
          underAnnex: this.data.constructionImageArr.join(',')
        },
        success: res => {
          if (res.data.code == 0) {
            wx.showToast({
              title: '删除成功',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }, 500)
  },
  // 添加已完工图片
  finishedAdd(e) {
    wx.chooseImage({
      success: res => {
        var finishedImageArr = [];
        res.tempFilePaths.forEach((value, index) => {
          wx.uploadFile({
            url: this.data.IAP + 'oss/upload',
            filePath: value,
            name: 'file',
            success: res => {
              finishedImageArr.push(JSON.parse(res.data).url[0]);
            },
          })
        })
        setTimeout(() => {
          if (this.data.finishedImageArr) {
            this.setData({
              finishedImageArr: this.data.finishedImageArr.concat(finishedImageArr).splice(0, 9)
            })
          } else {
            this.setData({
              finishedImageArr: finishedImageArr
            })
          }
          var url = this.data.IAP
          if (this.data.electricType == '电杆' || this.data.electricType == '电缆') {
            url = url + 'electric/update'
          } else if (this.data.electricType == '连接线' || this.data.electricType == '架空线') {
            url = url + 'cable/update'
          }
          wx.request({
            url: url,
            header: {
              "token": wx.getStorageSync('token')
            },
            method: "POST",
            data: {
              electricId: this.data.electricId,
              completeAnnex: this.data.finishedImageArr.join(',')
            },
            success: res => {
              if (res.data.code == 0) {
                wx.showToast({
                  title: '上传成功',
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            }
          })
        }, 500)
      },
    })
  },
  // 删除已完工图片
  finishedClose(e) {
    var currentIndex = e.currentTarget.dataset.index;
    this.data.finishedImageArr.splice(currentIndex, 1)
    this.setData({
      finishedImageArr: this.data.finishedImageArr
    })
    setTimeout(() => {
      var url = this.data.IAP
      if (this.data.electricType == '电杆' || this.data.electricType == '电缆') {
        url = url + 'electric/update'
      } else if (this.data.electricType == '连接线' || this.data.electricType == '架空线') {
        url = url + 'cable/update'
      }
      wx.request({
        url: url,
        header: {
          "token": wx.getStorageSync('token')
        },
        method: "POST",
        data: {
          electricId: this.data.electricId,
          completeAnnex: this.data.finishedImageArr.join(',')
        },
        success: res => {
          if (res.data.code == 0) {
            wx.showToast({
              title: '删除成功',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }, 500)
  },
  // 放大查看图片
  enlarge(e) {
    var imageArray = e.currentTarget.dataset.arr;
    var imageIndex = e.currentTarget.dataset.index
    wx.previewImage({
      urls: imageArray,
      current: imageArray[imageIndex]
    })
  },
  // 获取电杆/电缆 连接线/架空线信息
  getElectricInfo(id, type) {
    wx.request({
      url: this.data.IAP + 'electric/info/' + id + '/' + type,
      header: {
        "token": wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.code == 0) {
          this.setData({
            electricInfo: res.data.obj,
            index: res.data.obj.consStatus,
            startImageArr: res.data.obj.startAnnex ? res.data.obj.startAnnex.split(',') : '',
            constructionImageArr: res.data.obj.underAnnex ? res.data.obj.underAnnex.split(',') : '',
            finishedImageArr: res.data.obj.completeAnnex ? res.data.obj.completeAnnex.split(',') : ''
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getAddress();
    wx.setNavigationBarTitle({
      title: options.name,
    })
    var type = 0;
    if (options.type == '电杆' || options.type == '电缆') {
      type = 0;
    } else if (options.type == '连接线' || options.type == '架空线') {
      type = 1
    }
    this.getElectricInfo(options.id, type)
    this.setData({
      electricType: options.type,
      electricId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})